package com.dyb.recuitment.controller;


import com.dyb.recuitment.common.Result;
import com.dyb.recuitment.entity.Admin;
import com.dyb.recuitment.entity.Setting;
import com.dyb.recuitment.entity.User;
import com.dyb.recuitment.service.IRegisterService;
import com.dyb.recuitment.service.ISettingService;
import com.dyb.recuitment.service.IUserLogService;
import com.dyb.recuitment.service.impl.SettingServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 邓宇斌
 * @since 2022-01-14
 */

@Api(tags = "用户注册(Register)")
@RestController
@RequestMapping("/register")
@ResponseBody
public class RegisterController {

    @Autowired
    private IRegisterService registerService;

    @Autowired
    private ISettingService settingService;

    @Autowired
    private IUserLogService userLogService;

    @ApiOperation(value = "用户注册", notes = "通过用户名、密码、用户类型注册  {\"username\":\"\",\"password\":\"\",\"type\":1}")
    @ApiImplicitParam(name = "user", value = "User类")
    @PostMapping("/user")
    public Object register(@RequestBody User user) {
        Object data = null;
        Setting setting = new Setting();

        try {
            data = registerService.register(user);
            if (data == null || (int) data == 0) {
                return Result.failure(data, "注册失败!");
            }
            setting.setUserId(user.getId());
            settingService.add(setting);
            return Result.success(data, "注册成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data, "该用户名已被注册!");
    }

    @ApiOperation(value = "管理员注册", notes = "通过管理员用户名、密码、用户类型注册  {\"username\":\"\",\"password\":\"\"}")
    @ApiImplicitParam(name = "admin", value = "Admin类")
    @PostMapping("/admin")
    public Object admin(@RequestBody Admin admin) {
        Object data = null;
        try {
            data = registerService.registerAdmin(admin);
            if (data == null || (int) data == 0) {
                return Result.failure(data, "注册失败!");
            }
            return Result.success(data, "注册成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data, "该用户名已被注册!");
    }

}
